from contextlib import asynccontextmanager

from dotenv import load_dotenv
from fastapi import FastAPI
from fastapi_mcp import FastApiMCP
from infrastructure.utils.env import get_env_host, get_env_port
from presentation import generator, mcp


@asynccontextmanager
async def lifespan(app: FastAPI):
    # Startup code
    print("Starting up...")
    yield
    # Shutdown code
    print("Shutting down...")


app = FastAPI(
    root_path='/api',
    lifespan=lifespan,
)

app.include_router(generator.router)
app.include_router(mcp.router)

mcp_server = FastApiMCP(
    app,
    name="tqt-activity-development-mcp-server",
    describe_full_response_schema=True,  # 在工具描述中包含完整的JSON架构
    include_tags=['mcp'],  # 自定义暴露的端点
)

mcp_server.mount()

# 刷新MCP服务器以包含新端点
mcp_server.setup_server()

if __name__ == '__main__':
    import uvicorn

    # 设置环境变量
    load_dotenv()
    # 启动服务
    uvicorn.run(
        app=app,
        host=get_env_host(),
        port=get_env_port(),
    )
